/*
 * Copyright (c) 2021 com.youkeyi. All rights reserved.
 *
 *  @author liuxiawang
 *  @location Shenzhen.China
 *  @date 4/8/21 11:28 AM
 */

package com.youkeyi.ddy.cloud.common.interceptor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Youkeyi自定义拦截器
 *
 * @Author liuxiawang
 * @Date 2021-04-08 11:28
 * @Location shenzhen.china
 */
@Configuration
public class YoukeyiInterceptorAutoConfigure extends WebMvcConfigurationSupport {

	@Value("${youkeyi.session.exclude.url.exclude:}")
	private String excludeUrlPatterns;
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 设置拦截path
		List<String> doInterceptorPath = Arrays.asList("/system/**", "/supply/chain/web/**", "/supply/chain/applet/**");
		// 设置排除拦截path
		List<String> sessionExcludePath = new ArrayList<>();
		if (StringUtils.isNotBlank(excludeUrlPatterns)) {
			String[] split = excludeUrlPatterns.split(",");
			if (split.length > 0) {
				sessionExcludePath.addAll(Arrays.asList(split));
			}
		}

		// 添加登录校验拦截器
		registry.addInterceptor(new LoginValidateInterceptor())
				.addPathPatterns(doInterceptorPath)
				.excludePathPatterns(sessionExcludePath)
		;
		// 添加上下文拦截器
		registry.addInterceptor(new SystemContextInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns(sessionExcludePath)
		;
		// 添加功能角色权限拦截器
		registry.addInterceptor(new FunctionRolePermissionsInterceptor())
				.addPathPatterns(doInterceptorPath)
				.excludePathPatterns(sessionExcludePath)
		;
		// 添加防重复请求拦截器
		registry.addInterceptor(new RepeatRequestInterceptor())
				.addPathPatterns(doInterceptorPath)
				// .excludePathPatterns(sessionExcludePath)
		;
		// Open Api 校验签名拦截器
		registry.addInterceptor(new OpenApiSignInterceptor())
				.addPathPatterns("/openapi/**")
				.excludePathPatterns("/openapi/sign/sign")
		;
		super.addInterceptors(registry);
	}
}
